﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration.Install;
using System.ServiceProcess;
using System.IO;
using com.dz.ht.common;


namespace com.dz.ht.ui
{
    public partial class FrmServices : Form
    {
        /// <summary>
        /// 服务名称
        /// </summary>
        private string m_service_name = "dz_net_book";
        /// <summary>
        /// 服务文件名
        /// </summary>
        private string m_file_name = "HT-5.exe";

        public FrmServices()
        {
            InitializeComponent();

            m_file_name = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, m_file_name);
        }

      
        /// <summary>
        /// 注册服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_btn_register_Click(object sender, EventArgs e)
        {
            try
            {
                System.ServiceProcess.ServiceController t_service = new System.ServiceProcess.ServiceController(m_service_name);
                System.Collections.Hashtable i_state_saver = new System.Collections.Hashtable();
                if (!ServiceIsExisted(m_service_name))
                {
                    AssemblyInstaller t_assembly_installer = new AssemblyInstaller();
                    t_assembly_installer.UseNewContext = true;
                    t_assembly_installer.Path = m_file_name;
                    t_assembly_installer.Install(i_state_saver);
                    t_assembly_installer.Commit(i_state_saver);
                    t_assembly_installer.Dispose();
                    t_service.Start();
                    t_txt_info.Text = "服务注册成功，已在运行";
                }
                else
                {
                    if (t_service.Status != System.ServiceProcess.ServiceControllerStatus.Running && t_service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                    {
                        t_service.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("安装服务失败" + ex.ToString());
                t_txt_info.Text = "安装服务失败,请查看日志";
            }
        }

    

      

        /// <summary>
        /// 判断服务是否存在
        /// </summary>
        /// <param name="i_service_name"></param>
        /// <returns></returns>
        private bool ServiceIsExisted(string i_service_name)
        {
            ServiceController[] t_services = ServiceController.GetServices();
            foreach (ServiceController s in t_services)
            {
                if (s.ServiceName == i_service_name)
                {
                    return true;
                }
            }
            return false;
        }



      


        /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_btn_start_Click(object sender, EventArgs e)
        {
            if (ServiceIsExisted(m_service_name))
            {
                System.ServiceProcess.ServiceController t_service = new System.ServiceProcess.ServiceController(m_service_name);
                if (t_service.Status != System.ServiceProcess.ServiceControllerStatus.Running && t_service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                {
                    t_service.Start();
                    for (int i = 0; i < 60; i++)
                    {
                        t_service.Refresh();
                        System.Threading.Thread.Sleep(1000);
                        if (t_service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                        {
                            break;
                        }
                        if (i == 59)
                        {
                            LogHelper.Error("启动服务失败");
                            t_txt_info.Text = "启动服务失败";
                        }
                    }
                }
                t_txt_info.Text = "服务已启动,正在运行";
            }
            else
            {
                t_txt_info.Text = "服务不存在,请先安装";
            }

        }

        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_btn_stop_Click(object sender, EventArgs e)
        {
            try
            {
                if (ServiceIsExisted(m_service_name))
                {
                    System.ServiceProcess.ServiceController t_service = new System.ServiceProcess.ServiceController(m_service_name);
                    if (t_service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        t_service.Stop();
                        for (int i = 0; i < 60; i++)
                        {
                            t_service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (t_service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                            {
                                break;
                            }
                            if (i == 59)
                            {
                                LogHelper.Error("停止服务失败");
                            }
                        }
                    }

                    t_txt_info.Text = "服务已停止";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("停止服务失败" + ex.ToString());
                t_txt_info.Text = "服务停止失败";
            }
        }

        /// <summary>
        /// 卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_btn_delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (ServiceIsExisted(m_service_name))
                {

                    AssemblyInstaller t_assembly_installer = new AssemblyInstaller();
                    t_assembly_installer.UseNewContext = true;
                    t_assembly_installer.Path = m_file_name;
                    t_assembly_installer.Uninstall(null);
                    t_assembly_installer.Dispose();
                    t_txt_info.Text = "服务已卸载";
                }
                else
                {
                    t_txt_info.Text = "服务并不存在";
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("卸载服务失败" + ex.ToString());
                t_txt_info.Text = "卸载服务失败,请查看日志";
            }
        }

        /// <summary>
        /// 查询服务状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void t_btn_status_Click(object sender, EventArgs e)
        {
            if (ServiceIsExisted(m_service_name))
            {
                System.ServiceProcess.ServiceController t_service = new System.ServiceProcess.ServiceController(m_service_name);
                if (t_service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    t_txt_info.Text = "服务正在运行";
                }
                else if (t_service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                {
                    t_txt_info.Text = "服务已停止";
                }
            }
            else
            {
                t_txt_info.Text = "服务并不存在";
            }

        }


    }
}
